import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:smart_ai_dart/routers/app_pages.dart';
import 'package:smart_ai_dart/utils/storage.dart';

class AuthMiddleware extends GetMiddleware {
  @override
  int? get priority => 1;

  @override
  RouteSettings? redirect(String? route) {
    // 获取存储的token
    final token = StorageUtils.shared.getString('jwt_token');

    // 如果是登录页面且已登录，重定向到首页
    if (route == Routes.LOGIN && token != null) {
      return const RouteSettings(name: Routes.MAIN);
    }

    // 如果不是登录页面且未登录，重定向到登录页
    if (route != Routes.LOGIN && token == null) {
      return const RouteSettings(name: Routes.LOGIN);
    }

    return null;
  }
}
